package com.example.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.common.R;
import com.example.entity.Admin;
import com.example.entity.Professional;
import com.example.entity.Project;
import com.example.entity.Scientific;
import com.example.service.AdminService;
import com.example.service.ProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("projects")
public class ProjectController {

    @Autowired
    private ProjectService projectService;

    @Autowired
    private AdminService adminService;


    /**
     * 分页查询所有申请信息
     * @return
     */
    @GetMapping("/getPageProject/{currentPage}/{pageSize}")
    public R getPage(@PathVariable int currentPage,@PathVariable int pageSize){

        LambdaQueryWrapper<Project> lqw=new LambdaQueryWrapper<>();
        lqw.eq(Project::getStatusOne,"通过");
        IPage page =new Page(currentPage,pageSize);

        IPage page1 = projectService.page(page);

       return R.success("分页查询所有申请信息",page1);
    }


    /**
     * 分页查询当前工号申请的项目信息
     * @return
     */

    @GetMapping("/getPageProject/{currentPage}/{pageSize}/{jobNumber}")
    public R getPageByJobNumber(@PathVariable int currentPage,@PathVariable int pageSize,@PathVariable Integer jobNumber,Project project){

        LambdaQueryWrapper<Project> lqw=new LambdaQueryWrapper<>();
        lqw.eq(Project::getJobNumber,jobNumber);
        lqw.like(Project::getName,project.getName());
        // select * from table where jobNumber=1232321 and name like %123%

        IPage page =new Page(currentPage,pageSize);
        IPage page1 = projectService.page(page,lqw);

        return R.success("分页查询当前工号申请的项目信息",page1);
    }

    /**
     * 通过专业分页查询（一级管理员查看所有自己专业的）
     * @return
     */

    @GetMapping("/getByMajorAndAdminJobNumber/{currentPage}/{pageSize}/{jobNumber}")

    public R getByMajorAndAdminJobNumber(@PathVariable int currentPage, @PathVariable int pageSize, @PathVariable int jobNumber){

        //获取当前管理员信息
        LambdaQueryWrapper<Admin> lqwAdmin= new LambdaQueryWrapper<>();
        lqwAdmin.eq(Admin::getJobNumber,jobNumber);
        Admin one = adminService.getOne(lqwAdmin);


        //根据管理员的专业获得对应专业的论文申请
        LambdaQueryWrapper<Project> lqw=new LambdaQueryWrapper<>();
        lqw.eq(Project::getMajor,one.getMajor());

        IPage page =new Page(currentPage,pageSize);
        IPage page1 = projectService.page(page,lqw);

        return R.success("通过专业分页查询（带员工工号参数的条件查询）",page1);
    }

    /**
     * 添加申请项目
     * @param project
     * @return
     */
    @PostMapping("/addProject")
    public R add(@RequestBody Project project){

        projectService.save(project);
        return R.success("添加申请项目",project);
    }

    /**
     * 通过id删除申请项目
     * @param id
     * @return
     */
    @DeleteMapping("/deleteProject/{id}")
    public R deleteById(@PathVariable int id){

        boolean b = projectService.removeById(id);
        if (b){
            return R.success("通过id删除申请项目成功！",null);
        }else{
            return R.error("通过id删除申请项目失败！");
        }
    }

    /**
     * 教职工修改个人申请项目
     * @param project
     * @return
     */
    @PutMapping("/updateProject")
    public R updateProject(@RequestBody Project project){

        boolean b = projectService.updateById(project);
        if (b){
            return R.success("教职工修改个人申请项目成功",project);
        }else {
            return R.error("教职工修改个人申请项目失败");
        }
    }
}

